﻿/*
 * -------------------------------MD5转换算法-------------------------
 * 时间:2018/11/08
 * 作者:范德澄
 * 邮件:457343315@1qq.com
 * 描述：这是MD5转换算法代码
 */
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Web;

namespace MD5EncryptionDevice
{
    public class MD5Arithmetic
    {
        /// <summary>
        /// string转换成ulong
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static ulong String2Ulong(string key)
        {
            //字符串装换byte数组
            byte[] data = new UnicodeEncoding().GetBytes(key);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] dataBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(data);
            //字节数组指定八个字节转换64位无符号整数   a、b的结果是二进制码
            var a = BitConverter.ToUInt64(dataBytes, 0);
            var b = BitConverter.ToUInt64(dataBytes, 8);
            //^：异或运算
            ulong hashCode = a ^ b;
            return hashCode;
        }

        /// <summary>
        /// string转换成Int  返回0~(startNum-1)的范围内的整数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="startNum"></param>
        /// <returns>0~(startNum-1)的范围内的整数</returns>
        public static int String2Int(string key, int startNum)
        {
            //字符串装换byte数组
            byte[] data = new UnicodeEncoding().GetBytes(key);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] dataBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(data);
            //字节数组指定八个字节转换64位无符号整数   a、b的结果是二进制码
            var a = BitConverter.ToUInt64(dataBytes, 0);
            var b = BitConverter.ToUInt64(dataBytes, 8);
            //^：异或运算
            ulong hashCode = a ^ b;
            return Convert.ToInt32(hashCode % Convert.ToUInt64(startNum));
        }

        /// <summary>
        /// string转换成Int 返回startNum~endNum的范围内的整数
        /// </summary>
        /// <param name="key"></param>
        /// <param name="i"></param>
        /// <returns>startNum~endNum的范围内的整数</returns>
        public static int String2Int(string key, int startNum, int endNum)
        {

            //字符串装换byte数组
            byte[] data = new UnicodeEncoding().GetBytes(key);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] dataBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(data);
            //字节数组指定八个字节转换64位无符号整数   a、b的结果是二进制码
            var a = BitConverter.ToUInt64(dataBytes, 0);
            var b = BitConverter.ToUInt64(dataBytes, 8);
            //^：异或运算
            ulong hashCode = a ^ b;
            return Convert.ToInt32(hashCode % Convert.ToUInt64(endNum)) + startNum;
        }

        /// <summary>
        /// 加密算法
        /// MD5加密是不可逆的，所以只有加密没有解密
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string String2String(string key)
        {
            //字符串转换byte数组
            byte[] data = new UnicodeEncoding().GetBytes(key);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] hashedBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(data);
            //把byte数组转换为字符串后返回，BitConverter用于将基础数据类型与字节数组相互转换
            return BitConverter.ToString(hashedBytes);
        }

        /// <summary>
        /// int转换string
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string Int2String(int key)
        {
            //整型装换byte数组
            byte[] data = BitConverter.GetBytes(key);
            //由明文的byte数组计算出MD5密文byte数组
            byte[] dataBytes = ((HashAlgorithm)CryptoConfig.CreateFromName("MD5")).ComputeHash(data);
            string str = BitConverter.ToString(dataBytes);
            return str;
        }
    }
}
